/*===============================================
				Excel
===============================================*/
var	c_offset_flag = -2;
function FC(pSheet, nRowStart, nRowEnd, nColAdd, nColAddress, strCityName, strCityPrefix)
{
	this.pSheet	= pSheet;
	this.nRowStart = nRowStart;
	this.nRowEnd = nRowEnd;

	this.nColAdd = nColAdd;
	
	this.nColFandian = 3;		//饭店全名列
	this.nColAddress = nColAddress;
	this.strCityName = strCityName;
	this.strCityPrefix = strCityPrefix;

	this.nColFandianKey = nColAdd+5;
	this.nColAddressKey = nColAdd+6;
	this.nColTelKey = 5;

	this.nRowNoCur = 0;

	//抓取数据用到
	this.pSheetDest = null;
	this.nDestStartRow = 2;

}

FC.prototype.SetCurRow=function(nRowNoCur)
{
	this.nRowNoCur = nRowNoCur;
}

/*---------------------------------------------------------------
	P1 : 	FC.fandian->coord
	功能 : 获取所有已在mapabc地图中的饭店坐标;
	说明 : 返回的数据grade - 
				100(电话匹配),
				96(分店匹配),	
				90(名称和地址匹配),
				60(名称匹配,并且库中只有一个),
				50|40(名称匹配，存在多个相关的，随便选了一个)
---------------------------------------------------------------*/
//-判断是否需要继续处理 针对:P1-
FC.prototype.IsNeedDo=function(nRowNoCur)
{
	var bNeed = false;
	var strFlag= GetCellText(this.pSheet, nRowNoCur, this.nColAdd+c_offset_flag);
/*	if(		strFlag=="10" || strFlag=="100" ||	strFlag=="90" || strFlag=="96" 
		||	strFlag=="80" || strFlag=="70"	
		||	strFlag=="60" || strFlag=="50"	||	strFlag=="40" 
		||	strFlag=="gl_poi")
//*/
	if(strFlag=="")
		bNeed = true;

	return bNeed;
}

//获取电话号码，如果存在区号,不要区号
FC.prototype.getSrcTel=function(nRow)
{
	var strTel = GetCellText(this.pSheet, nRow, this.nColTelKey);
	if(strTel!="")
	{
		var strSet = strTel.split("-");
		if(strSet.length>1)
			strTel = strSet[1];
	}	

	return strTel;
}

//-如果电话一致，则一定就是-
FC.prototype.getFandianRtn_tel=function(strTel,data)
{
	var rtn = null;
	var obj = null;
	for (var i=0; i<data.poilist.length; i++)
	{	
		obj = data.poilist[i];
		if(obj.tel!="" && obj.tel.indexOf(strTel)>=0)
		{
			rtn = obj;
			break;
		}
	}

	return rtn;
}

//-如果饭店分店完全匹配-
FC.prototype.getFandian_Fendian=function(strFandianAll, oList)
{
	var strDianLeft = "";
	var strDianRight = ""
	var rtn = null;
	var obj = null;
	for (var i=0; i<oList.length; i++)
	{	
		obj = oList[i];

		//-如果名称完全一致-
		if(LCS_Fandian(strFandianAll, obj.name))
		{
			rtn = obj;
			break;
		}			
	}

	return rtn;
}

//-如果饭店名称和地址都匹配，一定是高精度匹配-
FC.prototype.getFandianRtn_NameAddress=function(strFandian, strAddress, oList)
{
	var rtn = null;
	var obj = null;
	for (var i=0; i<oList.length; i++)
	{	
		obj = oList[i];
		if(obj.address=="" || obj.address.indexOf(strAddress)<0)
			continue;
		
		rtn = obj;
		break;		
	}

	return rtn;
}

//-不匹配的地址当中，如果仅有一个，就选它-
FC.prototype.getFandianRtn_NameNoAddress=function(strAddress, oList)
{
	var rtn = null;
	var obj = null;
	var count = 0;
	for (var i=0; i<oList.length; i++)
	{	
		obj = oList[i];
		if(obj.address!="")
			continue;

		rtn = obj;
		count++;
		if(count>1)
		{
			rtn = null;
			break;
		}
	}

	return rtn;
}

//- 获取匹配饭店名称的集合-
FC.prototype.getFandianNameList=function(strFandian, data)
{
	var oList = [];
	
	var obj = null;
	var count = 0;
	for (var i=0; i<data.poilist.length; i++)
	{	
		obj = data.poilist[i];
		if(obj.name.indexOf(strFandian)>=0)
			oList.push(obj);
	}

	return oList;
}

//-判断结果数据是否一致-
FC.prototype.getFandianRtn=function(data)
{
	var obj = null;
	var grade = 0;

	//-1.判断电话号码是否一致-
	var strTel = this.getSrcTel(this.nRowNoCur);
	if(strTel!="")
	{
		obj = this.getFandianRtn_tel(strTel, data);
		if(obj)
		{
			grade = 100;
		}
	}

	if(grade==0)
	{
		//-获取匹配的饭店集合-
		var strFandian = GetCellText(this.pSheet, this.nRowNoCur, this.nColFandianKey);
		var oList = this.getFandianNameList(strFandian,data);
		if(oList.length>0)
		{
			//-2.0如果分店匹配-
			var strFlag = GetCellText(this.pSheet, this.nRowNoCur, this.nColAdd);	//是否分店标记
			if(strFlag=="1")
			{
				var strFandianAll = GetCellText(this.pSheet, this.nRowNoCur, this.nColFandian);
				obj = this.getFandian_Fendian(strFandianAll, oList);
				if(obj)
					grade = 96;
			}

			//-2.1如果名称和地址都匹配-
			if(grade==0)
			{
				var strAddress = GetCellText(this.pSheet, this.nRowNoCur, this.nColAddressKey);
				if(strAddress!="")
				{
					obj = this.getFandianRtn_NameAddress(strFandian, strAddress, oList);
					if(obj)
					{
						grade = 90;			
					}					
				}
			}
			
			
			//-3 仅有名称匹配时-
			if(grade==0)
			{
				//-3.2 只有一个时-
				if(oList.length==1)
				{
					obj = oList[0];
					grade = 60;		
				}
				else
				{
					//-3.1 地址不匹配中地址为空的情况-
					obj = this.getFandianRtn_NameNoAddress(strAddress, oList);
					if(obj)
						grade = 50;
					else
					{
						obj = oList[0];
						grade = 40;			//-有多个名称匹配的,其实此时可以依次判断每个地址，判断是否一致，但是不做了-
					}
				}
			}			
		}
	}
	
	if(obj)
	{
		SetCellText(this.pSheet, this.nRowNoCur, this.nColAdd+c_offset_flag, grade);
		SetCellText(this.pSheet, this.nRowNoCur, this.nColAdd+1, getDecodeCoordinate(obj.x)+","+getDecodeCoordinate(obj.y));
		SetCellText(this.pSheet, this.nRowNoCur, this.nColAdd+2, obj.name);
		SetCellText(this.pSheet, this.nRowNoCur, this.nColAdd+3, obj.address);
		SetCellText(this.pSheet, this.nRowNoCur, this.nColAdd+4, obj.tel);
	}
}

//-获取某个查询结果-
FC.prototype.fnCallback_Fandian=function(data)
{	
	var _this_ = m_pObj;
	if(data.message=="ok" && data.poilist.length>0)
	{		
		_this_.getFandianRtn(data);
	}

	_this_.SearchFandian(_this_.nRowNoCur+1);
}

//-查找饭店-
FC.prototype.SearchFandian=function(nRowCur)
{
	var strFandianKey;
	for (var i=nRowCur; i<=this.nRowEnd; i++)
	{
		//-如果已处理，忽略-
		if(!this.IsNeedDo(i))
			continue;
		
		//--
		strFandianKey = GetCellText(this.pSheet,i,this.nColFandianKey);
		if(strFandianKey=="")
			continue;

		document.getElementById("txtRowStart").value = i;
		this.SetCurRow(i);
		KeySearch(this.fnCallback_Fandian, this.strCityName, strFandianKey, "餐饮服务");
		break;
	}

	if(i==this.nRowEnd)	
		alert("处理完毕!");
}

/*-----------------------------------------------
	P2 :	FC.coord->address
	功能 : 根据当前坐标点调整坐标精度 
	说明 : 对于grade=40-60并且没有地址的饭店,
			通过坐标获取地址,再与地址匹配;
			如果地址匹配，grade改为80
-----------------------------------------------*/
//-判断是否需要获取地址-
FC.prototype.IsNeedGetAddress=function(nRowNoCur)
{
	var strAddress = GetCellText(this.pSheet, nRowNoCur, this.nColAdd+3);
	if(strAddress!="")
		return false;

	var bNeed   = false;
	var strFlag = GetCellText(this.pSheet, nRowNoCur, this.nColAdd+c_offset_flag);
	if(strFlag!="")
	{
		var nFlag = parseInt(strFlag);
		if(!isNaN(nFlag))
		{
			if (nFlag>=40 && nFlag<=60)
			{
				bNeed = true;
			}
		}
	}
	return bNeed;
}

//-获取某个查询结果-
FC.prototype.getCoordArress=function(data)
{
	//-获取地址-
	var sp = data.SpatialBean;
	var strAddress = sp.Province.name + sp.City.name + sp.District.name;
	if(sp.crossPoiList)
		strAddress += sp.crossPoiList[0].name;
	strAddress = strAddress + "("+sp.poiList["0"].address + sp.poiList["0"].name+")";

	//-判断该地址是否存在-
	var	grade=0;
	var strAddressKey = GetCellText(this.pSheet, this.nRowNoCur, this.nColAddressKey);
	if(strAddressKey!="")
	{
		if(strAddress.indexOf(strAddressKey)>=0)
			grade = 80;
	}

	if(grade==80)
		SetCellText(this.pSheet, this.nRowNoCur, this.nColAdd+c_offset_flag, grade);
	SetCellText(this.pSheet, this.nRowNoCur, this.nColAdd+3, strAddress);
}

//-获取某个地址-
FC.prototype.fnCallback_Address=function(data)
{	
	var _this_ = m_pObj;
	if (data.message == "ok") 
	{
		_this_.getCoordArress(data);
	}

	_this_.SearchAddress(_this_.nRowNoCur+1);
}

//-查找地址-
FC.prototype.SearchAddress=function(nRowCur)
{
	var strCoord;
	var strCoordSet;
	for (var i=nRowCur; i<=this.nRowEnd; i++)
	{
		//-如果已处理，忽略-
		if(!this.IsNeedGetAddress(i))
			continue;
		
		//--
		strCoord = GetCellText(this.pSheet,i,this.nColAdd+1);
		if(strCoord=="")
			continue;

		strCoordSet = strCoord.split(",");
		if(strCoordSet.length<2)
			continue;
	
		document.getElementById("txtRowStart").value = i;
		this.SetCurRow(i);
		SearchXzxqName(this.fnCallback_Address, strCoordSet);
		break;
	}

	if(i==this.nRowEnd)	
		alert("处理完毕!");
}

/*-----------------------------------------------
	P3 : 	FC.grade->update
	功能 : 根据地址调整精度
	说明 : 对于grade=40-60,
			如果存在地址相同的部分,将grade改为70
-----------------------------------------------*/
//-判断是否需要获取地址-
FC.prototype.IsNeedUpdateAddressGrade=function(nRowNoCur)
{
	var bNeed   = false;
	var strFlag = GetCellText(this.pSheet, nRowNoCur, this.nColAdd+c_offset_flag);
	if(strFlag!="")
	{
		var nFlag = parseInt(strFlag);
		if(!isNaN(nFlag))
		{
			if (nFlag>=40 && nFlag<=60)
			{
				bNeed = true;
			}
		}
	}
	
	return bNeed;
}

FC.prototype.UpdateAddressGrade=function()
{
	var strAddress;
	var strAddressNew;
	var	strSameText;
	for (var i=this.nRowStart; i<=this.nRowEnd; i++)
	{
		//-如果已处理，忽略-
		if(!this.IsNeedUpdateAddressGrade(i))
			continue;
		
		//--		
		strAddress = GetCellText(this.pSheet, i, this.nColAddress);
		strAddressNew = GetCellText(this.pSheet, i, this.nColAdd+3);
		if(strAddress=="" || strAddressNew=="")
			continue;

		document.getElementById("txtRowStart").value = i;		
		
		strSameText = LCS_Address_Ex(this.strCityName, strAddress, strAddressNew);
		if(strSameText!="")
		{			
			SetCellText(this.pSheet, i, this.nColAdd+c_offset_flag, 70);
		}

/*chy-check 2011-10-08 老的比较相似方法		
		strSameText = document.body.external.getSameText(strAddress, strAddressNew);
		if(strSameText.length<2)
			continue;
		
		strSameText = getShortName(this.strCityName, strSameText);
		if(strSameText.length>1 && strAddress.indexOf(strSameText)>=0 && strAddressNew.indexOf(strSameText)>=0)
		{			
			SetCellText(this.pSheet, i, this.nColAdd+c_offset_flag, 70);
		}
//*/
	}

	alert("处理完毕!");
}

/*-----------------------------------------------
		P4 : FC.Address->Coord
	功能 : 对于精度级别不高的数据，根据地址获取坐标
-----------------------------------------------*/
//-判断是否需要继续处理 针对:P4-
FC.prototype.IsNeedDo4=function(nRowNoCur)
{
	var bNeed = false;
	var strFlag= GetCellText(this.pSheet, nRowNoCur, this.nColAdd+c_offset_flag);
	if(strFlag=="")
		bNeed = true;
	else
	{
		var nFlag = parseInt(strFlag);
		if(!isNaN(nFlag))
		{
			if (nFlag>=40 && nFlag<=60)
			{
				bNeed = true;
			}
		}
	}

	return bNeed;
}

//-获取某个查询结果-
FC.prototype.fnCallback=function(x, y, grade, title)
{	
	var _this_ = m_pObj;

	SetCellText(_this_.pSheet, _this_.nRowNoCur, _this_.nColAdd+c_offset_flag, grade);
	SetCellText(_this_.pSheet, _this_.nRowNoCur, _this_.nColAdd+1, getDecodeCoordinate(x)+","+getDecodeCoordinate(y));
	if(title)
		SetCellText(_this_.pSheet, _this_.nRowNoCur, _this_.nColAdd+3, title);

	_this_.ContinueSearch(_this_.nRowNoCur+1);
}

//-继续查找标记不为空的-
FC.prototype.ContinueSearch=function(nRowCur)
{
	var strAddress;
	for (var i=nRowCur; i<=this.nRowEnd; i++)
	{
		if(!this.IsNeedDo4(i))
			continue;
		strAddress = GetCellText(this.pSheet,i,this.nColAddress);
		if(strAddress=="")
			continue;

		document.getElementById("txtRowStart").value = i;
		this.SetCurRow(i);
		SearchAddress_All(this.fnCallback, strAddress, this.strCityName);
		break;
	}

	if(i==this.nRowEnd)	
		alert("处理完毕!");
}

/*-----------------------------------------------
		P5 : FC.Range->Data
	功能 : 根据区间范围抓取数据
	说明 : 
-----------------------------------------------*/
FC.prototype.IsNeedRangeDo=function(nRowNo)
{
	var bNeed = true;
	var strFlag= GetCellText(this.pSheet, nRowNo, 8);
	if(	strFlag!="")
		bNeed = false;

	return bNeed;	
}

FC.prototype.getRangeSearchRtn=function(data)
{
	var obj, strIndex;
	var pDestSheet = this.pSheetDest;
	var nRow;
	SetCellText(this.pSheet, this.nRowNoCur, 9, data.searchresult.list.length);
	for (var i = 0; i < data.searchresult.list.length; i++) 
	{
		//当前目的行
		nRow = this.nDestStartRow;
		
		//当前行数据
		obj = data.searchresult.list[i];
		strIndex = 	GetCellText(this.pSheet, this.nRowNoCur, 7);
		SetCellText(pDestSheet, nRow, 2, strIndex);
		SetCellText(pDestSheet, nRow, 3, obj.citycode);
		SetCellText(pDestSheet, nRow, 4, obj.name);
		SetCellText(pDestSheet, nRow, 5, obj.address);
		SetCellText(pDestSheet, nRow, 6, obj.tel);
		SetCellText(pDestSheet, nRow, 7, obj.x+","+obj.y);
		SetCellText(pDestSheet, nRow, 8, obj.id);
		SetCellText(pDestSheet, nRow, 9, obj.pguid);
		SetCellText(pDestSheet, nRow, 10, obj.xml);
		SetCellText(pDestSheet, nRow, 11, obj.type);
		SetCellText(this.pSheet, this.nRowNoCur, 8, 1);
		//置为下一行
		this.nDestStartRow += 1;
		document.getElementById("txtDestStartRow").value = this.nDestStartRow;
	}
}

//-获取某个查询结果-
FC.prototype.fnCallback_RangeSearch=function(data)
{	
	var _this_ = m_pObj;
	if(data.message=="ok" && data.searchresult.record!=0)
	{		
		_this_.getRangeSearchRtn(data);
	}
	else
		SetCellText(_this_.pSheet, _this_.nRowNoCur, 8, "-");
	_this_.RangeSearchData(_this_.nRowNoCur+1);
}

//-查找饭店-
FC.prototype.RangeSearchData=function(nRowCur)
{
	var strCoords;
	var lnglats;
	for (var i=nRowCur; i<=this.nRowEnd; i++)
	{
		//-如果已处理，忽略-
		if(!this.IsNeedRangeDo(i))
			continue;
		
		//-获取坐标-
		strCoords = GetCellText(this.pSheet, i, 6);
		if(strCoords=="")
			continue;

		document.getElementById("txtRowStart2").value = i;
		this.SetCurRow(i);

		lnglats=getLngLats(strCoords);
		RangeSearch(this.fnCallback_RangeSearch, "", lnglats, "餐饮服务");
		break;
	}

	if(i==this.nRowEnd)	
		alert("处理完毕!");
}

/*===============================================
				Global info
===============================================*/
var m_pWks = null;
function getCurWks()
{
	var pWks = null;

	var strUrl = GetLastExcelUrl();

	if(m_pWks)
		pWks = m_pWks;
	else
	{
		pWks = document.body.external.openWks(strUrl, true);
		m_pWks = pWks;
	}
	if (!pWks)
	{
		alert("打开excel文件失败");
	}

	return pWks;
}

function getCurSheet()
{
	var pWks = getCurWks();
	if(!pWks)
		return;

	//-获取sheet name-
	var strSheetName = document.getElementById("txtSheet").value;
	if(strSheetName=="")
	{
		var cbo = document.getElementById("cboSheet");
		var x=cbo.selectedIndex;
		strSheetName = cbo.options[x].text;
	}
	if(strSheetName=="")
	{
		alert("请指定SheetName");
		return null;
	}

	var pSheet = GetSheet(pWks, strSheetName);
	if(!pSheet)
	{
		alert("获取指定Sheet - "+strSheetName+ " 失败!");
	}

	return pSheet;
}

/*===============================================
				获取坐标
===============================================*/
//-城市切换-
function city_onchange()
{
	document.getElementById("txtCityPrefix").value = document.getElementById("cityId").value;
}

//-获取选中excel sheet的行数、地址列-
function cboSheet_onchange()
{
	var oSelect = document.getElementById("cboSheet");
	
	if(oSelect.selectedIndex>=0)	
	{
		var oRowEnd = document.getElementById("txtRowEnd");
		oRowEnd.value = m_oInfoSet[oSelect.selectedIndex].row.toString();
		
		var oColAddress = document.getElementById("txtColAddress");
		oColAddress.value = m_oInfoSet[oSelect.selectedIndex].addresscol.toString();
	}
}

//-获取最后文件名-
function GetLastExcelUrl()
{
	var strUrl;
	var strCur = document.getElementById("txtExcel").value;
	if(strCur.indexOf(":")>0)
		strUrl = strCur;
	else
	{
		var strAll = window.location.href;
		strAll = strAll.replace(/\\/g, "/");
		var nPos = strAll.lastIndexOf("/");
		var str1 = strAll.substring(0,nPos+1);
		strUrl = str1+strCur;
	}

	return strUrl;
}

//-获取坐标的提取信息excel文件信息-
var m_oInfoSet = [];
function btnPickExcel_onclick()
{
	var strUrl = GetLastExcelUrl();
	var pWks = document.body.external.openWks(strUrl,false);
	if (!pWks)
	{
		alert("打开excel文件失败");
		return;
	}
	m_oInfoSet.length = 0;
	m_oInfoSet = GetAllSheetNameSet(pWks);

	var oSelect = document.getElementById("cboSheet");
	oSelect.options.length = 0;
	for (i=0; i<m_oInfoSet.length; i++)
	{
		var o=document.createElement('option');
		o.text=m_oInfoSet[i].name;
		oSelect.add(o);
	}
	oSelect.selectedIndex = 0;
	cboSheet_onchange();
	document.body.external.closeWks(pWks);

	CollectGarbage();
}

var m_pObj = null;

//P1 : 根据POI获取坐标
function btnDoPoi_onclick()
{
	//-获取参数-
	var nRowStart = parseInt(document.getElementById("txtRowStart").value);
	var nRowEnd = parseInt(document.getElementById("txtRowEnd").value);
	var nColAddress = parseInt(document.getElementById("txtColAddress").value);
	var nColAdd = parseInt(document.getElementById("txtColAdd").value);
	if(nRowStart<=0 || nRowEnd<=0 || nRowStart>nRowEnd || nColAddress<=0 || nColAdd<=0)
	{
		alert("请填写正确参数!");
		return;
	}

	var pSheet = getCurSheet();
	if(!pSheet)
		return;
	
	var strCityName = document.getElementById("cityId").value;
	var strCityPrefix = document.getElementById("txtCityPrefix").value;
	m_pObj = new FC(pSheet, nRowStart, nRowEnd, nColAdd, nColAddress, strCityName, strCityPrefix);	
	m_pObj.SearchFandian(nRowStart);
}

//P2 : 根据坐标获取地址
function btnDoCoord_onclick()
{
	//-获取参数-
	var nRowStart = parseInt(document.getElementById("txtRowStart").value);
	var nRowEnd = parseInt(document.getElementById("txtRowEnd").value);
	var nColAddress = parseInt(document.getElementById("txtColAddress").value);
	var nColAdd = parseInt(document.getElementById("txtColAdd").value);
	if(nRowStart<=0 || nRowEnd<=0 || nRowStart>nRowEnd || nColAddress<=0 || nColAdd<=0)
	{
		alert("请填写正确参数!");
		return;
	}

	var pSheet = getCurSheet();
	if(!pSheet)
		return;
	
	var strCityName = document.getElementById("cityId").value;
	var strCityPrefix = document.getElementById("txtCityPrefix").value;
	m_pObj = new FC(pSheet, nRowStart, nRowEnd, nColAdd, nColAddress, strCityName, strCityPrefix);	
	m_pObj.SearchAddress(nRowStart);
}

//P3 : 根据地址调整精度
function btnDoAdjust_onclick()
{
	//-获取参数-
	var nRowStart = parseInt(document.getElementById("txtRowStart").value);
	var nRowEnd = parseInt(document.getElementById("txtRowEnd").value);
	var nColAddress = parseInt(document.getElementById("txtColAddress").value);
	var nColAdd = parseInt(document.getElementById("txtColAdd").value);
	if(nRowStart<=0 || nRowEnd<=0 || nRowStart>nRowEnd || nColAddress<=0 || nColAdd<=0)
	{
		alert("请填写正确参数!");
		return;
	}

	var pSheet = getCurSheet();
	if(!pSheet)
		return;
	
	var strCityName = document.getElementById("cityId").value;
	var strCityPrefix = document.getElementById("txtCityPrefix").value;
	var obj = new FC(pSheet, nRowStart, nRowEnd, nColAdd, nColAddress, strCityName, strCityPrefix);	
	obj.UpdateAddressGrade();
}

//P4 : 根据地址获取坐标
function btnDoAddress_onclick()
{
	//-获取参数-
	var nRowStart = parseInt(document.getElementById("txtRowStart").value);
	var nRowEnd = parseInt(document.getElementById("txtRowEnd").value);
	var nColAddress = parseInt(document.getElementById("txtColAddress").value);
	var nColAdd = parseInt(document.getElementById("txtColAdd").value);
	if(nRowStart<=0 || nRowEnd<=0 || nRowStart>nRowEnd || nColAddress<=0 || nColAdd<=0)
	{
		alert("请填写正确参数!");
		return;
	}

	var pSheet = getCurSheet();
	if(!pSheet)
		return null;

	var strCityName = document.getElementById("cityId").value;
	var strCityPrefix = document.getElementById("txtCityPrefix").value;
	m_pObj = new FC(pSheet, nRowStart, nRowEnd, nColAdd, nColAddress, strCityName, strCityPrefix);	
	m_pObj.ContinueSearch(nRowStart);

	//CloseWks(pWks, true);
}

//------------------------------------------------------------
//					抓取数据
//------------------------------------------------------------
var m_pWks2 = null;
function getCurWks2()
{
	var pWks = null;

	var strUrl = document.getElementById("txtExcel2").value;

	if(m_pWks2)
		pWks = m_pWks2;
	else
	{
		pWks = document.body.external.openWks(strUrl, true);
		m_pWks2 = pWks;
	}
	if (!pWks)
	{
		alert("打开excel文件失败");
	}

	return pWks;
}

function getCurSheet2(strSheetName)
{
	var pWks = getCurWks2();
	if(!pWks)
		return;

	var pSheet = GetSheet(pWks, strSheetName);
	return pSheet;
}

var m_oInfoSet2 = [];

//-获取选中excel sheet的行数、地址列-
function cboDataSrc_onchange()
{
	var oSelect = document.getElementById("cboDataSrc");
	
	if(oSelect.selectedIndex>=0)	
	{
		var oRowEnd = document.getElementById("txtRowEnd2");
		oRowEnd.value = m_oInfoSet2[oSelect.selectedIndex].addresscol.toString();
	}
}

//-抓取数据的提取信息excel文件信息-
function btnPickExcel2_onclick()
{
	var strUrl = document.getElementById("txtExcel2").value;
	var pWks = document.body.external.openWks(strUrl,false);
	if (!pWks)
	{
		alert("打开excel文件失败");
		return;
	}
	
	m_oInfoSet2.length = 0;
	m_oInfoSet2 = GetAllSheetNameSet(pWks, true);

	var oSelectSrc  = document.getElementById("cboDataSrc");
	var oSelectDest = document.getElementById("cboDataDest");
		oSelectSrc.options.length = 0;
		oSelectDest.options.length = 0;
	var o1,o2;
	for (var i=0; i<m_oInfoSet2.length; i++)
	{
		o1=document.createElement('option');
		o1.text=m_oInfoSet2[i].name;
		oSelectSrc.add(o1);

		o2=document.createElement('option');
		o2.text=m_oInfoSet2[i].name;
		oSelectDest.add(o2);
	}
	oSelectSrc.selectedIndex = m_oInfoSet2.length-2;
	oSelectDest.selectedIndex = m_oInfoSet2.length-1;
	cboDataSrc_onchange();

	document.body.external.closeWks(pWks);
	CollectGarbage();
}

//P5 : - 抓取数据-
function btnDoData_onclick()
{
	/*test-chy
	var s1 = "旅顺大学城海鲜街45号";
	var s2 = "海鲜街";
	var bb = LCS_Address_Ex("大连市",s1,s2);
	alert(bb);
	return;
	//*/

	//-获取参数-
	var nRowStart = parseInt(document.getElementById("txtRowStart2").value);
	var nRowEnd = parseInt(document.getElementById("txtRowEnd2").value);
	if(nRowStart<=0 || nRowEnd<=0 || nRowStart>nRowEnd)
	{
		alert("请填写正确参数!");
		return;
	}

	//-获取数据源-
	var x=document.getElementById("cboDataSrc").selectedIndex;
	var strSheetName = document.getElementById("cboDataSrc").options[x].text;
	var pSheet = getCurSheet2(strSheetName);
	if(!pSheet)
	{
		alert("cboDataSrc no!");
		return;
	}

	//-获取目标数据源-
	x=document.getElementById("cboDataDest").selectedIndex;
	strSheetName = document.getElementById("cboDataDest").options[x].text;
	var pSheetDest = getCurSheet2(strSheetName);
	if(!pSheetDest)
	{
		alert("cboDataDest no!");
		return;
	}

	m_pObj = new FC(pSheet, nRowStart, nRowEnd);	
	m_pObj.pSheetDest = pSheetDest;
	m_pObj.nDestStartRow = parseInt(document.getElementById("txtDestStartRow").value);

	m_pObj.RangeSearchData(nRowStart);
}

$(document).ready(function () 
{
	$("#btnPickExcel").bind("click", btnPickExcel_onclick);

	$("#btnDoPoi").bind("click", btnDoPoi_onclick);
	$("#btnDoCoord").bind("click", btnDoCoord_onclick);
	$("#btnDoAdjust").bind("click", btnDoAdjust_onclick);
	$("#btnDoAddress").bind("click", btnDoAddress_onclick);

	$("#btnPickExcel2").bind("click", btnPickExcel2_onclick);
	$("#btnDoData").bind("click", btnDoData_onclick);
	
});